package org.jaaksi.kottlindemo.test

import android.animation.ObjectAnimator
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.animation.doOnEnd
import androidx.fragment.app.Fragment
import extension.argument
import org.jaaksi.kottlindemo.databinding.FragmentTestBinding
import org.jetbrains.anko.dip
import org.jetbrains.anko.toast

class TestFragment : Fragment() {
    private lateinit var binding: FragmentTestBinding
    private val dp_100 by lazy { requireContext().dip(100).toFloat() }
    private val age by argument(20)
    private val id2 by argument(key = "ID", defaultValue = 10)

    override fun onAttach(context: Context) {
        super.onAttach(context)
//        println(">> onAttach")
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        println(">> onCreate")
        println(">> age = $age, id2 = $id2")
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
//        println(">> onCreateView")
        binding = FragmentTestBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
//        println(">> onViewCreated")
        test()
    }

    private fun test() {
        ObjectAnimator.ofFloat(binding.btnClick, "translationX", dp_100)
            .setDuration(5000)
            .apply {
                doOnEnd {
                    // isAdded方法是mHost != null && mAdded;
                    println(">> isAdded = $isAdded, host = $activity")
                    if (!isAdded) return@doOnEnd
                    context?.toast("Anim end")
//                    requireContext().toast("Anim end")
                }
            }
            .start()
//        requireActivity().finish()
    }


    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
//        println(">> onActivityCreated")
    }


    // 在onDestroy之后回调的
    override fun onDetach() {
//        println(">> onDetach")
        super.onDetach()
    }

    override fun onDestroyView() {
//        println(">> onDestroyView")
        super.onDestroyView()
    }

    override fun onDestroy() {
//        println(">> onDestroy")
        super.onDestroy()
    }
}